package com.zhang.first.day07;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @title:
 * @author: zhang
 * @date: 2022/1/20 20:07
 */
public class Test1 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .socketTextStream("localhost", 9999)
                .map(r -> Tuple2.of(r.split(" ")[0], r.split(" ")[1]))
                .returns(Types.TUPLE(Types.STRING, Types.STRING))
                .keyBy(r -> r.f0)
                .process(new KeyedProcessFunction<String, Tuple2<String, String>, String>() {
                    @Override
                    public void processElement(Tuple2<String, String> value, KeyedProcessFunction<String, Tuple2<String, String>, String>.Context ctx, Collector<String> out) throws Exception {
                        out.collect("key:" + ctx.getCurrentKey() + "\t,并行子任务插槽：" + getRuntimeContext().getIndexOfThisSubtask());
                    }
                }).setParallelism(4)
                .print();


        env.execute();
    }
}
